package 线程.作业.卖票;

import java.util.concurrent.locks.ReentrantLock;

public class maipiao02 implements Runnable{
    private static int piao=99;
    private static ReentrantLock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            if (lock.tryLock()){
                //判断有没有锁,有就继续
                try {
                    //手动抛出错误
                    if(piao>0){
                        //有票
                        System.out.println(Thread.currentThread().getName()+"正在卖第"+piao+"张票");
                        System.out.println(Thread.currentThread().getName()+"还剩下"+--piao+"张票");
                    }else {
                        //没有
                        break;
                    }
                } catch (Exception e) {
                    //解决方法
                    e.printStackTrace();
                } finally {
                    //就算没解决也要解锁
                lock.unlock();
                }
            }
        }
    }
}
